package com.zhdl.modules.prefiresys.task;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.HexUtil;
import com.zhdl.modules.prefiresys.entity.DetectorAreaInfo;
import com.zhdl.modules.prefiresys.service.DetectorAreaInfoService;
import com.zhdl.modules.prefiresys.web.service.CollectService;
import com.zhdl.network.prefiretcp.FramePkg;
import com.zhdl.network.prefiretcp.before.MemDevice;
import com.zhdl.network.servertcp.DuplexProtocolSendService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import static com.zhdl.modules.prefiresys.web.service.CollectService.currentTime;

/**
 * @Author: zhangyy
 * @Email: zhang10092009@hotmail.com
 * @Date: 2023/7/13 15:18:56
 * @Version: v1.0
 * @Modified:
 * @Description:
 */
@Slf4j
@Component
public class PrefireWarnTaskDemo {

    @Resource
    private DetectorAreaInfoService detectorAreaInfoService;

    @Resource
    private CollectService collectService;

    public Map<String,String> maps = new ConcurrentHashMap<>();

    @Scheduled(fixedRate = 600000000,initialDelay = 1000)
    public void init() {
        List<DetectorAreaInfo> allNoBy =
                detectorAreaInfoService.findAllNoBy();

        for (DetectorAreaInfo detectorAreaInfo : allNoBy) {
            maps.put(detectorAreaInfo.getId(),"");
        }
    }


    @Scheduled(fixedRate = 60 * 1000,initialDelay = 1000)
    public void task() {
//        for (String areaId : maps.keySet()) {
//            String contLastRunTime = maps.get(areaId);
//            String contRunTime = collectService.sseResp.getAreaList().get(areaId).getContRunTime();
//            if (!contLastRunTime.equals(contRunTime)){
//                maps.put(areaId,contRunTime);
//            } else {
//                collectService.sseResp.getAreaList().get(areaId).setOnlineStatus(false);
//                maps.put(areaId,"");
//            }
//        }
        for (String areaId : maps.keySet()) {
            String contLastRunTime = maps.get(areaId);
            String contRunTime = currentTime.get(areaId);
            if (!contLastRunTime.equals(contRunTime)){
                maps.put(areaId,contRunTime);
            } else {
                collectService.sseResp.getAreaList().get(areaId).setOnlineStatus(false);
                maps.put(areaId,"");
            }
        }
    }
    @Scheduled(cron = "0 0 12 * * ?")
    public void taskSendAll() throws IOException {
//        for (MemDevice device : collectService.memDeviceList.values()) {
//            byte[] bytes = device.getPkg().GeneralParamMonitor();
//            DuplexProtocolSendService.sendCmdToAll("0000", HexUtil.encodeHexStr(bytes));
//        }
        DuplexProtocolSendService.sendCmdToAll("0000", HexUtil.encodeHexStr(new FramePkg((short) 0,false).GeneralParamMonitor()));
        String curDateTime = DateUtil.now();
        log.info("taskSendAll-->{} ---> collectService.memDeviceList.values()的个数为:{}",curDateTime, collectService.memDeviceList.values().size());
    }

}
